package com.sit.library.controller;

import com.sit.library.entity.bean.Account;
import com.sit.library.entity.bean.Book;
import com.sit.library.entity.bean.RestBean;
import com.sit.library.entity.vo.response.AccountVO;
import com.sit.library.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping("/find/user")
    public String findUser() {
        List<AccountVO> accountsVO = adminService.getAccounts();
        return RestBean.success(accountsVO).asJsonString();
    }

    @PostMapping("/add/user")
    public String addUser(@RequestBody Account account) {
        String result = adminService.addAccount(account);
        return RestBean.success(result).asJsonString();
    }

    @PostMapping("/edit/user")
    public String editUser(@RequestBody Account account) {
        String result = adminService.updateAccount(account);
        return RestBean.success(result).asJsonString();
    }

    @DeleteMapping("/delete/user/{userId}")
    public String deleteUser(@PathVariable int userId) {
        String result = adminService.deleteAccount(userId);
        return RestBean.success(result).asJsonString();
    }

    @GetMapping("/find/book")
    public String findBook() {
        List<Book> books = adminService.getBooks();
        return RestBean.success(books).asJsonString();
    }

    @PostMapping("/add/book")
    public String addBook(@RequestBody Book book) {
        String result = adminService.addBook(book);
        return RestBean.success(result).asJsonString();
    }

    @PostMapping("/edit/book")
    public String editBook(@RequestBody Book book) {
        String result = adminService.updateBook(book);
        return RestBean.success(result).asJsonString();
    }

    @DeleteMapping("/delete/book/{bookId}")
    public String deleteBook(@PathVariable int bookId) {
        String result = adminService.deleteBook(bookId);
        return RestBean.success(result).asJsonString();
    }
}
